<?php
namespace app\adminapiv3\controller;

use app\common\service\Base as AppBase;
use think\facade\Request;
class Base extends AppBase
{
    function __construct()
    {
        global $errCode;
        $errCode = $this->CODE_SUCCESS;
        global $errMsg;
        $errMsg = '操作已成功!';
        global $logContent;
        $logContent = '';//日志操作
        $this->setHostId();
    }




    /**
     * @name: json输出
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-05-22 16:31:42
     */
    public function jsonOutPut ($data = [], $is_call = false)
    {
        global $errCode;
        global $errMsg;
        $msg = $is_call && $errCode == $this->CODE_SUCCESS ? '正在下发命令至客户端,若命令未生效请检查通信日志!' : '';
        $outData = [
            'code' => $errCode,
            'msg' => $errMsg . $msg,
            'request_date' => date('Y-m-d H:i:s'),
        ];


        //添加操作日志
        $server = new \app\common\service\ServerOptionHistory();
        $server->addUserLog();

        isset($data['data']) ? $outData = array_merge($outData, $data) : $outData['data'] = $data;
        echo json_encode($outData, JSON_UNESCAPED_UNICODE);
    }

    /**
     * @name: 获取每页条数
     * @Param: 111
     * @Description: 
     * @Author: foo
     * @Date: 2019-04-23 17:57:42
     */
    public function getLimit ()
    {
        $limit = Request::param('limit', 10);
        return $limit;
    }

    /**
     * @name: 过滤 信息输入
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-05-22 17:46:03
     */
    public function filterInput ($index, $method = 'post', $default = '')
    {

        $hav = false;
        if (strstr($index, '*'))
        {
            $index = substr($index,0, -1);
            $hav = true;
        }

        if (empty($index) || $index == 'data')
        {
            $inputData = $this->clearEmoji(Request::$method($index, $default));
        }
        else
        {
            $inputData = $this->clearEmoji(Request::$method($index, $default));
        }

        if ($hav && (empty($inputData) && !is_numeric($inputData)))
        {

            echo json_encode([
                'code' => 201,
                'msg' => '[' . strtoupper($index) . ']' . '为必须参数',
            ],JSON_UNESCAPED_UNICODE);
            die;
        }

        if(is_array($inputData)){
            return $inputData;
        }


        return trim($inputData);
    }

    /**
     * @name: 清除emoji
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-21 13:43:00
     */
    function clearEmoji ($str)
    {
        $return = preg_replace_callback(    //执行一个正则表达式搜索并且使用一个回调进行替换
            '/./u',
            function (array $match) {
                return strlen($match[0]) >= 4 ? '' : $match[0];
            }, $str);

        return $return;
    }

    /**
     * @name: 设置主机id
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-15 13:54:01
     */
    private function setHostId ()
    {
        $host_id = $this->filterInput('host_id', 'param');

        if($host_id === ''){//没有host_id,跳过验证
            return true;
        }


        preg_match_all('/,?(\d+),?/i',$host_id, $host_id_a);

        if(empty($host_id_a))
        {
            returnErrJson($this->CODE_FAIL,'错误的主机id');
        }

        if(strlen($host_id) != strlen(implode(',', $host_id_a[1])))
        {
            returnErrJson($this->CODE_FAIL,'错误的主机id');
        }
        $Host = new \app\common\service\Host();
        $online = $Host->hostList($host_id_a[1], 'id,name,os,osversion,ip,group_id');


        $exsitHost = array_keys($online);

        $notExsiHost = array();
        foreach ($host_id_a[1] as $v){
            if (!in_array($v, $exsitHost)) {
                $notExsiHost[] = $v;
            }
        }

        if ($notExsiHost)
        {
            returnErrJson($this->CODE_FAIL,'主机id为:' . implode('，', $notExsiHost) . '不存在或已被删除');
        }
        global $online_host;

        $online_host = $online;

    }


    /**
     * @name: 重新设置在线主机
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-01-13 18:24:26
     */
    function resetOnlineHost ($host)
    {
        global $online_host;
        $online_host = $host;
    }

}